<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import="java.util.Calendar"
    import="java.sql.*"
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>歸還場地設備</title>
</head>
<body>
<% if(session.getAttribute("access") == "y" ){%>
<jsp:useBean id="mydb" scope="page" class="bean.ConDbBean"/> <%//連結資料庫的bean %>
<%

//修改借用資料
String date=""; 
Calendar cal =Calendar.getInstance(); 
String y1 = ""; //查詢開始年份 
String m1 = ""; //查詢開始月份 
String d1 = ""; //查詢開始日期 
y1 = String.valueOf(cal.get(cal.YEAR)); 
m1=String.valueOf(cal.get(cal.MONTH)+1); 
d1 =String.valueOf(cal.get(cal.DATE));
date=y1+"/"+m1+"/"+d1;
String sql;
sql="UPDATE BORROW SET RETURN_DATE='"+date+"',RETURN_PERSON='"+session.getAttribute("accessid")+"' WHERE PF_ID='"+request.getParameter("pf_id")+"'";
mydb.executeUpdate(sql);

//修改逾期資料
sql="UPDATE DELAY SET RETURN_DATE='"+date+"',PAY='1' WHERE PF_ID='"+request.getParameter("pf_id")+"'";
mydb.executeUpdate(sql);

//修改會員資料
ResultSet rs;
sql="select * from MEMBER WHERE M_ID='"+request.getParameter("m_id")+"'";
rs=mydb.executeQuery(sql);
rs.next();
int delay_day=java.lang.Integer.valueOf(rs.getString("DELAY_DAY"));//最大逾期天數
int delay_item=java.lang.Integer.valueOf(rs.getString("DELAY_ITEM"));//逾期的次數
int damage_pf=java.lang.Integer.valueOf(rs.getString("DAMAGE_PF"));//破壞的次數
if(java.lang.Integer.valueOf(request.getParameter("bad"))==1){//表示場地設備歸還時遭到破壞
	damage_pf++;
}
if(java.lang.Integer.valueOf(request.getParameter("delay_day"))>0){//表示此次有逾期
	delay_item++;
}
if(java.lang.Integer.valueOf(request.getParameter("delay_day"))>delay_day){//本次逾期的天數大於資料庫逾期天數,則儲存此次天數
	delay_day=java.lang.Integer.valueOf(request.getParameter("delay_day"));
}
sql="UPDATE MEMBER SET DELAY_DAY='"+delay_day+"',DELAY_ITEM='"+delay_item+"',DAMAGE_PF='"+damage_pf+"' WHERE M_ID='"+request.getParameter("m_id")+"'";
mydb.executeUpdate(sql);

//修改場地設備狀態
sql="select * from PLACE WHERE P_ID='"+request.getParameter("pf_id")+"'";
rs=mydb.executeQuery(sql);
if(rs.next()){//為場地資料
	sql="UPDATE PLACE SET P_STATE='00' WHERE P_ID='"+request.getParameter("pf_id")+"'";
	mydb.executeUpdate(sql);
}else{//為設備資料
	sql="select * from FACILITY WHERE F_ID='"+request.getParameter("pf_id")+"'";
	rs=mydb.executeQuery(sql);
	if(rs.next()){
		sql="UPDATE FACILITY SET F_STATE='00' WHERE F_ID='"+request.getParameter("pf_id")+"'";
		mydb.executeUpdate(sql);
	}
}


response.sendRedirect("lookReturn.jsp"); //直接跳頁到lookFacility.jsp
%>

<%
  }
  else{
  	response.sendRedirect("../index.jsp"); //若沒有登入則回到index.jsp
  } 
  %>
</body>
</html>